package com.game.client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.game.share.IOPackage;
import com.game.share.StreamReader;
import com.game.share.Util;

public class Main {

	private static void testForGame2() throws IOException {
		Client client = new Client("192.168.1.202");
		new Thread(client).start();
		IOPackage pkg = client.send(ActionGenerator.getLoginAction("yedawei",
				"123456"));
		String str = Util.byteToString(pkg.getData());
		if (str.length() == 0) {
			return;
		}
		System.out.println(str);
		pkg = client.send(ActionGenerator.GetGameListAction());
		StreamReader reader = new StreamReader(new ByteArrayInputStream(pkg.getData()));
		
		boolean isJoin = false;
		
		boolean isWait = false;
		
		while(!isJoin){
			int gameCount = reader.readInt();
			if (gameCount == 0) {
				if(!isWait){
					int rand = (int)(Math.random() * 5000);
					sleep(rand);
					pkg = client.send(ActionGenerator.GetGameListAction());
					reader = new StreamReader(new ByteArrayInputStream(pkg.getData()));
					isWait = true;
				} else {
					// create game
					// isJoin = true;
				}
				
				//wait for random seconds 
				// if have no game
				//		create game 
				// else // have some game 
				// 		if all game is full 
				// 			create game
				// 		else 
				//			join game
			} else {
				ArrayList<House> list = new ArrayList<House>();
				for (int i = 0; i < gameCount; i++) {
					House house = readHouse(reader);
					list.add(house);
				}
				for(House house : list){
					if(house.getMaxCount() > house.getUserCount()){
						//join
						
						//isJoin = true;
					}
				}
				if(!isJoin){
					
				}
			}
		}
		
		
		
	}
	
	private static House readHouse(StreamReader reader) throws IOException{
		int gameID = reader.readInt();
		String ownerName = reader.readString();
		int userCount = reader.readInt();
		int maxCount = reader.readInt();
		return new House(gameID, ownerName, userCount, maxCount);
	}
	

	private static void testForGame() {
		for (int i = 0; i < 10; i++) {
			new Thread() {
				public void run() {
					try {
						testForGame2();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws UnknownHostException
	 */
	public static void main(String[] args) {
		testForGame();
	}

	@SuppressWarnings("unused")
	private static void test3() {
		for (int i = 0; i < 20; i++) {
			new Thread(new Runnable() {
				public void run() {
					try {
						test2();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
	}

	public static void test2() throws IOException {
		Client client = new Client("192.168.1.202");
		new Thread(client).start();
		PrintCallback callback = new PrintCallback();

		client.send(ActionGenerator.getLoginAction("yedawei", "123456"),
				callback);

		sleep(1000);

		client.send(ActionGenerator.getLoginAction("ye.dawei", "123456"),
				callback);

		sleep(1000);

		client.send(ActionGenerator.getValidateAction("ye.dawei"), callback);
		sleep(1000);

		client.send(ActionGenerator.getValidateAction(null), callback);
		sleep(1000);

		client.send(ActionGenerator.getValidateAction(""), callback);
		sleep(1000);
		client.close();
	}

	private static void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void test1(String[] args) {
		int count = 1;
		if (args.length > 0) {
			try {
				int t = Integer.parseInt(args[0]);
				if (t > 0) {
					count = t;
				}
			} catch (Exception e) {
			}
		}
		Thread[] threadArray = new Thread[count];
		for (int i = 0; i < count; i++) {
			threadArray[i] = new Thread(new Client("192.168.1.202"));
		}

		for (Thread thread : threadArray) {
			thread.start();
		}
	}

}
